//
//  TCMyFavouriteController.m
//  陶瓷
//
//  Created by 王戈 on 15/8/5.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "TCMyFavouriteController.h"

#import "DiscoverJobViewController.h"
#import "DiscoverShopViewController.h"
#import "DiscoverMarketViewController.h"
#import "TailDetailViewController.h"
#import "TCMyFavouriteCell.h"

#import "MJRefresh.h"

typedef NS_ENUM(NSInteger, RefreshType) {
    PullUpRefresh,
    PushDownRefresh,
};


@interface TCMyFavouriteController ()

@property (nonatomic, strong) NSMutableArray *favourites;

@property (nonatomic, strong) UIButton *cover;

@property (nonatomic, assign) int pn;

@end

@implementation TCMyFavouriteController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"我的关注";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.pn = 1;
    
    __weak UITableView *tableView = self.tableView;
    tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pn = 1;
        [self refreshMessages:PushDownRefresh];
    }];
    tableView.header.autoChangeAlpha = YES;
    tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _pn += 1;
        [self refreshMessages:PullUpRefresh];
    }];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationItem setTitle:@"我的关注"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationItem setTitle:@""];
    [self.cover removeFromSuperview];
    [SVProgressHUD dismiss];
}

- (UIButton *)cover {
    if (_cover == nil) {
        _cover = [[UIButton alloc] initWithFrame:self.view.bounds];
        _cover.backgroundColor = [UIColor clearColor];
    }
    return _cover;
}

- (void)viewDidAppear:(BOOL)animated
{
//    [self.tableView.header beginRefreshing];
    [self refreshMessages:PushDownRefresh];
}

- (void)refreshMessages:(RefreshType)type {
    [SVProgressHUD showWithStatus:@"正在收藏数据 请稍后.."];
    [self.view addSubview:self.cover];
    if (type == PushDownRefresh) {
        [[TCNetworkTool sharedNetTool] favouritetListWithPn:[NSString stringWithFormat:@"%d",self.pn] andSuccessBlocks:^(NSArray *resultArray) {
            if ([self.favourites count] > 0) {
                [self.favourites removeAllObjects];
            }
            self.favourites = [resultArray mutableCopy];
            [SVProgressHUD dismiss];
            [self.tableView.header endRefreshing];
            [self.tableView reloadData];
            [self.cover removeFromSuperview];
        } andFailureBlocks:^(NSString *failureMessage) {
            [SVProgressHUD dismiss];
            [self.tableView.header endRefreshing];
            [self.cover removeFromSuperview];
            [MBHUDHelper showWarningWithText:failureMessage];
        }];
    }
    else
    {
        [[TCNetworkTool sharedNetTool] favouritetListWithPn:[NSString stringWithFormat:@"%d",self.pn] andSuccessBlocks:^(NSArray *resultArray) {
            [self.favourites addObjectsFromArray:resultArray];
            [SVProgressHUD dismiss];
            [self.tableView.footer endRefreshing];
            [self.tableView reloadData];
            [self.cover removeFromSuperview];
        } andFailureBlocks:^(NSString *failureMessage) {
            [SVProgressHUD dismiss];
            [self.tableView.footer endRefreshing];
            [self.cover removeFromSuperview];
            [MBHUDHelper showWarningWithText:failureMessage];
        }];
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TCMyFavouriteCell *cell = [TCMyFavouriteCell cellWithTableView:tableView];
    cell.favourite = _favourites[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _favourites.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    TCMyFavouriteCell *cell = (TCMyFavouriteCell *)[tableView cellForRowAtIndexPath:indexPath];
    switch (cell.type) {
        case kTCCollectTypeRecruitment:
        {
            DiscoverJobViewController *jb = [[DiscoverJobViewController alloc] init];
            jb.sid = cell.favourite.Id;
            jb.isFavor = YES;
            [self.navigationController pushViewController:jb animated:YES];
        }
            break;
        case kTCCollectTypeRent:
        {
            DiscoverShopViewController *jb = [[DiscoverShopViewController alloc] init];
            jb.sid = cell.favourite.Id;
            jb.isFavor = YES;
            [self.navigationController pushViewController:jb animated:YES];
            
        }
            break;
        case kTCCollectTypeTailGoods:
        {
            TailDetailViewController *tailVC = [[TailDetailViewController alloc] init];
            tailVC.proid = cell.favourite.Id;
            tailVC.isFavor = YES;
            [self.navigationController pushViewController:tailVC animated:YES];
        }
            break;
        case kTCCollectTypeMarket:
        {
            DiscoverMarketViewController *market = [[DiscoverMarketViewController alloc] init];
            market.sid = cell.favourite.Id;
            market.isFavor = YES;
            [self.navigationController pushViewController:market animated:YES];
        }
        default:
            break;
    }
}


@end
